package com.hbter630.change.controller.admin;




import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbter630.change.entity.Order_;
import com.hbter630.change.entity.User;
import com.hbter630.change.service.OrderService;
import com.hbter630.change.service.UserService;
import com.hbter630.change.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.PushBuilder;

/**
 * @Title: HomeController
 * @Author 笑霸fianl
 * @Package com.hbter630.change.controller
 * @Date 2023/5/22 23:54
 * @描述:
 */
@RestController
@RequestMapping("/Admin/user")
@Api(tags = "后台用户管理")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private OrderService orderService;



    @PostMapping("login")
    @ApiOperation(notes = "登录",value = " 登录", httpMethod = "POST", response = Result.class)
    public Result login(@RequestBody User user, HttpServletRequest request){

        log.info("\n 登录的信息 {}",request.getRequestURI());
        Result login = userService.login(user, request);

        return login;
    }

    @GetMapping("/info")
    @ApiOperation(notes = "用户详情",value = " 用户详情", httpMethod = "GET", response = Result.class)
    public Result info(String token){

        return userService.info(token);
    }

    @PostMapping("logout")
    @ApiOperation(notes = "退出",value = " 退出", httpMethod = "POST", response = Result.class)
    public Result logout(){

      return userService.logout();
    }

//    list
    @GetMapping("/list")
    @ApiOperation(notes = "用户列表",value = " 用户列表", httpMethod = "GET", response = Result.class)
    public Result list(){
        log.info("\n========list");
      return  userService.getlist();
    }

    //按条件查询
    @PostMapping("/list")
    @ApiOperation(notes = "用户列表",value = " 用户列表", httpMethod = "POST", response = Result.class)
    public Result getListBytt(@RequestBody User user){

        return userService.getListBytt(user);
    }

    //分页查询
    @GetMapping("/list/{current}/{limit}")
    @ApiOperation(notes = "分页查询用户列表",value = " 分页查询用户列表", httpMethod = "GET", response = Result.class)
    public Result getListByPage(
                                @PathVariable("current") Long current,
                                @PathVariable("limit") Long limit){

        return userService.getListByPage(current,limit);
    }


    //按id查询
    @GetMapping("/getUserById/{id}")
    @ApiOperation(notes = "根据id查询用户",value = " 根据id查询用户", httpMethod = "GET", response = Result.class)
    public Result getUserById(@PathVariable("id") Integer id){

        User byId = userService.getById(id);
        return Result.ok().addData(byId)  ;
    }



    /**
     * handleDelete
     */
    @PostMapping ("/delete")
    @ApiOperation(notes = "删除用户根据id",value = " 删除用户根据id", httpMethod = "POST", response = Result.class)
    public Result deleteByid(@RequestBody String id){
        log.info("===========delete {}",id);
        return userService.delete(id);
    }


    @PostMapping ("/add")
    @ApiOperation(notes = "添加用户",value = "添加用户", httpMethod = "POST", response = Result.class)
    public Result addUser(@RequestBody User user){

        boolean b = userService.saveAndUpdata(user);
        return b? Result.ok():Result.failed();
    }

    /**
     * 分页查询订单列表
     */
    @GetMapping ("/Order/{current}/{limit}")
    @ApiOperation(notes = "分页查询订单列表",value = "分页查询订单列表", httpMethod = "GET", response = Result.class)
    public Result getOrderByPage(@PathVariable("current") long current,
                                 @PathVariable("limit") long limit){

        return orderService.getOrderByPage(current,limit);
    }

    /**
     * 设置订单状态
     */
    @GetMapping ("/Status/{id}/{sta}")
    @ApiOperation(notes = "分页查询订单列表",value = "分页查询订单列表", httpMethod = "GET", response = Result.class)
    public Result getOrderByPage(@PathVariable("id") String id,
                                 @PathVariable("sta") Integer sta){

        QueryWrapper<Order_> order_queryWrapper = new QueryWrapper<>();
        order_queryWrapper.eq("order_id",id);
        Order_ one = orderService.getOne(order_queryWrapper);
        System.out.println("===========");
        System.out.println(one);
         one.setStatus(sta);
        orderService.updateById(one);
       return Result.ok().addData(Result.ok());

    }








}
